
package com.workcell.workdata.xthings.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.workcell.workdata.xthings.constant.HardwareStateEnum;
import com.workcell.workdata.xthings.dto.BaseVo;
import com.workcell.workdata.xthings.entity.BaseFreezer;
import com.workcell.workdata.xthings.mapper.BaseFreezerMapper;
import com.workcell.workdata.xthings.service.BaseFreezerService;
import org.springframework.stereotype.Service;

/**
 * 冷冻机表
 *
 * @author pigx code generator
 * @date 2023-04-19 11:40:11
 */
@Service
public class BaseFreezerServiceImpl extends ServiceImpl<BaseFreezerMapper, BaseFreezer> implements BaseFreezerService {
	/**
	 * 模糊查询分页
	 * @param baseVo
	 * @return
	 */
	@Override
	public Page<BaseFreezer> vaguePage(BaseVo baseVo) {
		LambdaQueryWrapper<BaseFreezer> baseFreezerCondition = new QueryWrapper<BaseFreezer>().lambda()
				.and(ObjectUtil.isNotEmpty(baseVo.getKeyWord()), i -> i
						.like(BaseFreezer::getName,baseVo.getKeyWord())
						.or()
						.like(BaseFreezer::getManufacturer,baseVo.getKeyWord()))
				.eq(ObjectUtil.isNotEmpty(baseVo.getState()),BaseFreezer::getState,baseVo.getState())
				.and(ObjectUtil.isNotEmpty(baseVo.getStartTime()) || ObjectUtil.isNotEmpty(baseVo.getEndTime())
						, i -> i.between(BaseFreezer::getCreateTime,baseVo.getStartTime(),baseVo.getEndTime()));
		Page<BaseFreezer> page = new Page<>(baseVo.getPageNum(), baseVo.getPageSize());
		page.addOrder(OrderItem.desc("create_time"));
		return this.page(page,baseFreezerCondition);
	}

	/**
	 * 修改冷冻机型号基本信息
	 * @param baseFreezer
	 * @return
	 */
	@Override
	public Boolean updateBaseFreezer(BaseFreezer baseFreezer) {
		Assert.isTrue(ObjectUtil.isNotEmpty(baseFreezer.getId()),"ID不能为空");
		//basePowermeter.setState(null);
		// 查询该冷冻机的状态
		BaseFreezer baseFreezerOld = this.getById(baseFreezer.getId());
		Assert.isTrue(ObjectUtil.isNotEmpty(baseFreezerOld),"无该条记录，请核实您的操作");
		Assert.isTrue(HardwareStateEnum.DRAF.getCode().equals(baseFreezerOld.getState()),"状态为草稿才能修改");
		return this.updateById(baseFreezer);
	}

	/**
	 * 修改状态
	 * @param baseFreezer
	 * @return
	 */
	@Override
	public Boolean updateStateById(BaseFreezer baseFreezer) {
		Assert.isTrue(ObjectUtil.isNotEmpty(baseFreezer.getId()),"ID不能为空");
		Assert.isTrue(ObjectUtil.isNotEmpty(baseFreezer.getState()),"状态不能为空");
		// 查询该冷冻机的状态
		BaseFreezer baseFreezerOld = this.getById(baseFreezer.getId());
		Assert.isTrue(ObjectUtil.isNotEmpty(baseFreezerOld),"无该条记录，请核实您的操作");
		if (HardwareStateEnum.INUSE.getCode().equals(baseFreezerOld.getState())
				|| HardwareStateEnum.SUSPEND.getCode().equals(baseFreezerOld.getState())){
			if (HardwareStateEnum.DRAF.getCode().equals(baseFreezer.getState())){
				Assert.isTrue(false,"当状态为使用中或暂停中时，不能修改为草稿状态");
			}
		}
		return this.updateById(baseFreezer);
	}

	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@Override
	public Boolean delById(Long id) {
		Assert.isTrue(ObjectUtil.isNotEmpty(id),"ID不能为空");
		// 查询换热机的状态
		BaseFreezer baseFreezerOld = this.getById(id);
		Assert.isTrue(ObjectUtil.isNotEmpty(baseFreezerOld),"无该条记录，请核实您的操作");
		if (HardwareStateEnum.INUSE.getCode().equals(baseFreezerOld.getState())
				|| HardwareStateEnum.SUSPEND.getCode().equals(baseFreezerOld.getState())){
			Assert.isTrue(false,"当状态为使用中或暂停中时，不能删除该条记录");
		}
		// 删除记录
		return this.removeById(id);
	}
}
